package exttable.dbclasses;

import dbutils.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 28.05.11
 * Time: 14:38
 * To change this template use File | Settings | File Templates.
 */
public class ProbationersGroupsV2 {
    private int volunteerId;
    private String probationerCode;
    private int groupId;
    private int sexId;
    private int degreeOfInflammatoryId;
    private int processByLocalizationId;
    private boolean diseaseDurationMoreThenYear;
    private boolean alcoholic;
    private boolean narcomaniac;
    private List<Group> groupList = new ArrayList<Group>();

    public int getVolunteerId() {
        return volunteerId;
    }

    public void setVolunteerId(int volunteerId) {
        this.volunteerId = volunteerId;
    }

    public String getProbationerCode() {
        return probationerCode;
    }

    public void setProbationerCode(String probationerCode) {
        this.probationerCode = probationerCode;
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public int getSexId() {
        return sexId;
    }

    public void setSexId(int sexId) {
        this.sexId = sexId;
    }

    public int getDegreeOfInflammatoryId() {
        return degreeOfInflammatoryId;
    }

    public void setDegreeOfInflammatoryId(int degreeOfInflammatoryId) {
        this.degreeOfInflammatoryId = degreeOfInflammatoryId;
    }

    public int getProcessByLocalizationId() {
        return processByLocalizationId;
    }

    public void setProcessByLocalizationId(int processByLocalizationId) {
        this.processByLocalizationId = processByLocalizationId;
    }

    public boolean isDiseaseDurationMoreThenYear() {
        return diseaseDurationMoreThenYear;
    }

    public void setDiseaseDurationMoreThenYear(boolean diseaseDurationMoreThenYear) {
        this.diseaseDurationMoreThenYear = diseaseDurationMoreThenYear;
    }

    public boolean isAlcoholic() {
        return alcoholic;
    }

    public void setAlcoholic(boolean alcoholic) {
        this.alcoholic = alcoholic;
    }

    public boolean isNarcomaniac() {
        return narcomaniac;
    }

    public void setNarcomaniac(boolean narcomaniac) {
        this.narcomaniac = narcomaniac;
    }

    public List<Group> getGroupList() {
        return groupList;
    }

    public void setGroupList(List<Group> groupList) {
        this.groupList = groupList;
    }

    public ProbationersGroupsV2() {

    }

    @Override
    public String toString() {
        return probationerCode;
    }

    public static Group getGroupToProbationer(ProbationersGroupsV2 probationersGroups) {
//           задаем коэффициенты параметров при подсчете
        final int INFILT_TUB_VALUE = 1;
        final double INF_TUB_WEIGHT = 0.7;

        final int FIBRO_TUB_VALUE = 3;
        final double FIBRO_TUB_WEIGHT = 0.25;

        final int LOC_ONE_SIDE_VALUE = 1;
        final double LOC_ONE_SIDE_WEIGHT = 0.6;

        final int LOC_TWO_SIDE_VALUE = 2;
        final double LOC_TWO_SIDE_WEIGHT = 0.4;
        int groupId = 0;

//        наполняем map группами для определениея веса параметров
        Group toReturn = new Group();
        Map<Integer, Double> mapGroup = new HashMap<Integer, Double>(probationersGroups.getGroupList().size());
        try {
            Connection con = DbUtils.getDBConnectionGeneral();
            String inStr = "";
            for (Group g : probationersGroups.getGroupList()) {
                inStr = inStr + "'" + g.getGroupId() + "', ";
                PreparedStatement ps = con.prepareStatement("SELECT count(*) FROM probationers_groups where excluded=0 AND group_id = ?");
                ps.setInt(1, g.getGroupId());
                ResultSet rs = ps.executeQuery();
                rs.next();
                if (rs.getInt(1) < 40) {
                    mapGroup.put(g.getGroupId(), 0.0);
                    groupId = g.getGroupId();
                }
                rs.close();
            }


            inStr = inStr.substring(0, inStr.length() - 2);

            System.out.println("inStr " + inStr);
            PreparedStatement ps = con.prepareStatement("SELECT * FROM probationers_groups where group_id in (" + inStr + ") AND excluded = 0");
            ResultSet rs = ps.executeQuery();
//              соибраем вес параметров у групп, которые выбрал пользователь
            while (rs.next()) {
                groupId = rs.getInt("group_id");

                mapGroup.put(groupId, (mapGroup.get(groupId) + 1));
                if ((rs.getInt("degree_of_inflammatory_id") == INFILT_TUB_VALUE)
                        && (rs.getInt("degree_of_inflammatory_id") == probationersGroups.getDegreeOfInflammatoryId())) {
                    mapGroup.put(groupId, (mapGroup.get(groupId) + 1) * INF_TUB_WEIGHT);
                }
                if ((rs.getInt("degree_of_inflammatory_id") == FIBRO_TUB_VALUE)
                        && (rs.getInt("degree_of_inflammatory_id") == probationersGroups.getDegreeOfInflammatoryId())) {
                    mapGroup.put(groupId, (mapGroup.get(groupId) + 1) * FIBRO_TUB_WEIGHT);
                }
                if ((rs.getInt("process_by_localization_id") == LOC_ONE_SIDE_VALUE)
                        && (rs.getInt("process_by_localization_id") == probationersGroups.getProcessByLocalizationId())) {
                    mapGroup.put(groupId, (mapGroup.get(groupId) + 1) * LOC_ONE_SIDE_WEIGHT);
                }
                if ((rs.getInt("process_by_localization_id") == LOC_TWO_SIDE_VALUE)
                        && (rs.getInt("process_by_localization_id") == probationersGroups.getProcessByLocalizationId())) {
                    mapGroup.put(groupId, (mapGroup.get(groupId) + 1) * LOC_TWO_SIDE_WEIGHT);
                }

                if ((rs.getInt("process_by_localization_id") == LOC_TWO_SIDE_VALUE)
                        && (rs.getInt("process_by_localization_id") == probationersGroups.getProcessByLocalizationId())) {
                    mapGroup.put(groupId, (mapGroup.get(groupId) + 1) * LOC_ONE_SIDE_WEIGHT);
                }

                if (rs.getBoolean("disease_duration_more_then_year") == probationersGroups.isDiseaseDurationMoreThenYear()) {
                    mapGroup.put(groupId, (mapGroup.get(groupId) + 1));
                }
//                if (rs.getInt("sex_id") == probationersGroups.getSexId()) {
//                    mapGroup.put(groupId, (mapGroup.get(groupId) + 1));
//                }
                if (rs.getBoolean("alcoholic") == probationersGroups.isAlcoholic()) {
                    mapGroup.put(groupId, (mapGroup.get(groupId) + 1));
                }
                if (rs.getBoolean("narcomaniac") == probationersGroups.isNarcomaniac()) {
                    mapGroup.put(groupId, (mapGroup.get(groupId) + 1));
                }
            }
            double point = mapGroup.get(groupId);
            toReturn.setGroupId(groupId);
//            определяем у какой группы наименьшый вес, это группа и будет претендентом на добавление
            for (Map.Entry<Integer, Double> res : mapGroup.entrySet()) {

                System.out.println("gr: " + res.getKey() + " wht " + res.getValue());
                if (point > res.getValue()) {
                    point = res.getValue();
                    toReturn.setGroupId(res.getKey());
                }
            }

            // ставим код группы
            for (Group group : probationersGroups.getGroupList()) {
                if (group.getGroupId() == toReturn.getGroupId()) {
                    toReturn.setGroupCode(group.getGroupCode());
                }
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return toReturn;
    }
}
